from django.db import models

# Create your models here.
from app.utils import make_hash


class User(models.Model):
    userid = models.AutoField(primary_key=True, verbose_name='编号')
    username = models.CharField(max_length=20, unique=True, verbose_name='用户名')
    password = models.CharField(max_length=64, verbose_name='口令')
    tel = models.CharField(max_length=11, verbose_name='手机号')
    last_visit = models.DateTimeField(null=True, verbose_name='最后登陆时间')

    def save(self, *args, insert=True, **kwargs):
        if insert:
            self.password = make_hash(self.password)
        return super().save(*args, **kwargs)

    class Meta:
        db_table = 'tb_user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name


class Subject(models.Model):
    no = models.AutoField(primary_key=True)
    name = models.CharField('学科名称', max_length=30)
    intro = models.CharField('学科介绍', max_length=2000)
    is_hot = models.BooleanField('是否热门', default=False)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'tb_subject'
        verbose_name = '学科'
        verbose_name_plural = verbose_name
        ordering = ['no']


class Teacher(models.Model):
    no = models.AutoField(primary_key=True, verbose_name='编号')
    name = models.CharField(max_length=20, verbose_name='姓名')
    sex = models.BooleanField(max_length=1, choices=((0, '男'), (1, '女'),), verbose_name='性别')
    birth = models.DateField(verbose_name='出生日期')
    intro = models.CharField(max_length=2000, verbose_name='介绍')
    photo = models.CharField(max_length=512, verbose_name='图片')
    good_count = models.IntegerField(default=0, db_column='g_count', verbose_name='好评数')
    bad_count = models.IntegerField(default=0, db_column='b_count', verbose_name='差评数')
    subject = models.ForeignKey(to=Subject, on_delete=models.PROTECT, db_column='s_num', verbose_name='所属学科')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'tb_teachers'
        verbose_name = '老师'
        verbose_name_plural = verbose_name
